<template>
  <div class="classify-warp">
    <div class="classify-content">
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        :rules="rules"
        status-icon
      >
        <el-form-item label="分类名称:" prop="name">
          <el-input
            style="width: 220px"
            clearable
            v-model="ruleForm.name"
          ></el-input>
        </el-form-item>
        <el-form-item label="上级分类：" prop="from">
          <el-select
            v-model="ruleForm.from"
            clearable
            placeholder="请选择分类"
            disabled
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量单位：" prop="count">
          <el-input
            style="width: 220px"
            clearable
            v-model="ruleForm.count"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitAdd('ruleForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {  addSecond } from "@/http/commodity-management/classityApi";
export default {
  name: "AddClassify",
  data() {
    return {
      ruleForm: {
        name: "",
        count: "",
        from: "测试数据",
      },
      options: [],
      rules: {
        name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        count: [{ required: true, message: "请输入数量单位", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitAdd(ruleForm) {
      //新增二级分类
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          this.$confirm("是否确认此次操作?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "error",
          })
            .then(() => {
              addSecond(
                this.ruleForm.count,
                this.ruleForm.from,
                this.ruleForm.name
              ).then((res) => {
                console.log(this.ruleForm);
                console.log(res);
              });
              this.$router.push({ path: "/goods/lookSubordinate" });
              this.$message({
                message: "新增成功",
                type: "success",
              });
            })
            .catch((err) => {
              console.log("新增失败", err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  activated() {
    this.ruleForm.from = this.$route.query.row.name;
  },
};
</script>

<style scoped lang="scss">
@import "@/assets/sass/var.scss";
.classify-warp {
  .classify-content {
    width: 100%;
    margin: 50px;
  }
}
</style>